<template>
<div class="register_box">
  <div class="container">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
              <div class="head"><img src="../../assets/logo.png" alt="">
              </div>
              
        <el-form-item label="手机号" prop="phonenum">
          <el-input v-model="ruleForm.phonenum" style="width:300px" placeholder="11位手机号码"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input type="password" v-model="ruleForm.pass" style="width:300px" autocomplete="off" placeholder="长度为6-18个字符"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" style="width:300px" autocomplete="off" placeholder="长度为6-18个字符"></el-input>
        </el-form-item>
          <div class="tooll">
            <el-button type="primary" @click="submitForm('ruleForm')" style="letter-spacing:0.5em">注册</el-button>
          <el-button @click="resetForm('ruleForm')" style="letter-spacing:0.5em">重置</el-button>
          </div>
          <a href="javascript:void(0);" @click="reback" class="back" v-if="islogin">返回</a>
          <a href="javascript:void(0);" @click="gologin" class="back" v-if="!islogin">去登陆</a>
      </el-form>
  </div>
  </div>
</template>

<script>
import {register} from '../../api/login';
  export default {
    name:'Register',
    data() {
      var checkPhone = (rule, value, callback) => {
        // console.log(value);
        if (!value) {
          return callback(new Error('手机号不能为空'));
        }else if(value.toString().length!=11){
          return callback(new Error('请输入正确的手机号'));
        }
        else{
          callback();
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        }else if(value.length<6||value.length>18){
          callback(new Error('请输入正确位数的密码'))
        }
         else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        islogin:false,
        ruleForm: {
          pass: '',
          checkPass: '',
          phonenum:''
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          phonenum:[
            {validator: checkPhone, trigger: 'blur'}
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            register(this.ruleForm.phonenum,this.ruleForm.pass).then(res=>{
              if(res.data.code==0){
                this.$message({
                  type: 'success',
                  message: '注册成功!请登录'
                });
                setTimeout(() => {
                  this.$router.push('/login')
                }, 1000);
              }else{
                this.$message({
                  type: 'error',
                  message: res.data.msg
                });
              }
            })
          } else {
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      reback(){
        this.$router.go(-1)
      },
      gologin(){
        this.$router.push('/login')
      }
    }
  }
</script>

<style scoped>
.register_box{
  width: 100%;
  height: 100%;
  display: flex;
}
.container{
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url('../../assets/register.png');
  background-repeat:no-repeat;
  background-size:100% 100%;
  background-position: center;
  
}
.demo-ruleForm{
  background: rgba(255, 255, 255, 0.6);
  width: 400px;
  height: 400px;
  border: 5px;
  border-radius: 10px;
}
.tooll{
  display: flex;
  flex-direction: row;
  justify-content: center;
  margin-left: 10%;
  margin-bottom: 10px;
}
.back{
  text-decoration: none;
  float: right;
  font-size: 16px;
  margin-right: 5px;
  margin-bottom: 5px;
}
.head{
  display: flex;
  flex-direction: column;
  align-items: center;
}
.head img{
  width: 100%;
}
</style>